package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.Impl.LeansnewsDaoImpl;
import com.ysd.entity.Leansnews;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminNewsServlet extends BaseServlet {
	/*****
	 * 查询所有新闻详情内容
	 * localhost:8080/huaduoyoupin/adminNews?reqName=getNews
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int newsId=0;
		if(request.getParameter("newsId")!=null && !request.getParameter("newsId").equals("")) {
			newsId=Integer.parseInt(request.getParameter("newsId"));
		}
		String newsTitle=request.getParameter("newsTitle");
		String newsContent=request.getParameter("newsContent");
		String newsXiangQin=request.getParameter("newsXiangQin");
		
		int newsCatalogueId=0;
		if(request.getParameter("newsCatalogueId")!=null && !request.getParameter("newsCatalogueId").equals("")) {
			newsCatalogueId=Integer.parseInt(request.getParameter("newsCatalogueId"));
		}
		//根据接收的参数创建新闻对象
		Leansnews l=new Leansnews(newsId, newsTitle, newsContent, newsXiangQin, newsCatalogueId);
		int page=0;
		if(request.getParameter("page")!=null && !request.getParameter("page").equals("")) {
			page=Integer.parseInt(request.getParameter("page"));
		}
		int size=0;
		if(request.getParameter("size")!=null && !request.getParameter("size").equals("")) {
			size=Integer.parseInt(request.getParameter("size"));
		}
		LeansnewsDaoImpl lDao=new LeansnewsDaoImpl();//创建实现类对象
		List<Leansnews>  lList=lDao.getLeansLeansnewByWherePage(l, page, size);//获得所有新闻分类
		
		
		response.setCharacterEncoding("UTF-8");//设置编码集格式
		PrintWriter  out=response.getWriter();//创建out对象
		
		
		Gson gson=new Gson();//创建gson对象
		
		String res=gson.toJson(lList);//将lList转化为json字符串
		out.print(res);//返回数据
		
		out.flush();
		out.close();
	}
	//分页查询
	//localhost:8080/huaduoyoupin/adminNews?reqName=getAllNews
	public void getAllNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int newsId=0;
		if(request.getParameter("newsId")!=null && !request.getParameter("newsId").equals("")) {
			newsId=Integer.parseInt(request.getParameter("newsId"));
		}
		String newsTitle=request.getParameter("newsTitle");
		String newsContent=request.getParameter("newsContent");
		String newsXiangQin=request.getParameter("newsXiangQin");
		
		int newsCatalogueId=0;
		if(request.getParameter("newsCatalogueId")!=null && !request.getParameter("newsCatalogueId").equals("")) {
			newsCatalogueId=Integer.parseInt(request.getParameter("newsCatalogueId"));
		}
		//根据接收的参数创建新闻对象
		Leansnews l=new Leansnews(newsId, newsTitle, newsContent, newsXiangQin, newsCatalogueId);
		
		LeansnewsDaoImpl lDao=new LeansnewsDaoImpl();//创建实现类对象
		List<Leansnews>  lList=lDao.getLeansLeansnewByWhere(l);//获得所有新闻分类
		
		
		response.setCharacterEncoding("UTF-8");//设置编码集格式
		PrintWriter  out=response.getWriter();//创建out对象
		
		
		Gson gson=new Gson();//创建gson对象
		
		String res=gson.toJson(lList.size());//将lList转化为json字符串
		out.print(res);//返回数据
		
		out.flush();
		out.close();
	}
	/***
	 * 删除新闻详情
	 */
	public void delNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.parseInt(request.getParameter("newsId"));//接受前台传来的参数
		LeansnewsDaoImpl lDao=new LeansnewsDaoImpl();
		//设置编码集格式
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();//创建out对象
		
		int n=lDao.delNews(id);//调用实现类返回受影响行数
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		out.flush();
		out.close();
	}
    /****
     * 添加新闻详情
     */
	public void addNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		LeansnewsDaoImpl nDao=new LeansnewsDaoImpl();
		
		String  newsContent=request.getParameter("newsContent");
		String  newsXiangQin=request.getParameter("newsXiangQin");
		int newsCatalogueId=0;
		if(request.getParameter("newsCatalogueId")!=null && !request.getParameter("newsCatalogueId").equals("")) {
			newsCatalogueId=Integer.parseInt(request.getParameter("newsCatalogueId"));
		}
		String  newsTitle=request.getParameter("newsTitle");
		if(newsTitle!=null&& !newsTitle.equals("")) {
			List<Leansnews>  ncList=nDao.getNewsByTitle(newsTitle);
			if(ncList.size()==0) {
				Leansnews nc=new Leansnews(0, newsTitle, newsContent, newsXiangQin, newsCatalogueId);
				System.out.print(nc);
				
				int n=nDao.addNews(nc);
				if(n>0) {
					out.print(Result.toClient(true, "添加成功",""));
				}else {
					out.print(Result.toClient(false, "添加失败",""));
				}
			}else {
				out.print(Result.toClient(false, "标题已存在",""));
			}
		}
		
	
		out.flush();
		out.close();
	}
	/***
	 * localhost:8080/huaduoyoupin/adminNews?reqName=getLeansnewsById
	 */
	public void getLeansnewsById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		LeansnewsDaoImpl nDao=new LeansnewsDaoImpl();
		int lnId=Integer.parseInt(request.getParameter("nId"));
		Leansnews n=nDao.getLeansnewsById(lnId);
		
		Gson gson=new Gson();
		String res=gson.toJson(n);
		out.print(res);
		
		out.flush();
		out.close();
	}
	/**
	 * localhost:8080/huaduoyoupin/adminNews?reqName=updateLeansnewsById
	 * 修改新闻详情的内容
	 */
	public void updateLeansnewsById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		
		LeansnewsDaoImpl nDao=new LeansnewsDaoImpl();
		int newsId=0;
		if(request.getParameter("newsId")!=null && !request.getParameter("newsId").equals("")) {
			newsId=Integer.parseInt(request.getParameter("newsId"));
		}
		String newsTitle=request.getParameter("newsTitle");
		String newsContent=request.getParameter("newsContent");
		int newsCatalogueId=0;
		if(request.getParameter("newsCatalogueId")!=null && !request.getParameter("newsCatalogueId").equals("")) {
			newsCatalogueId=Integer.parseInt(request.getParameter("newsCatalogueId"));
		}
		if(newsTitle!=null&&!newsTitle.equals("")) {
			List<Leansnews>  ncList=nDao.getNewsByTitle(newsTitle);
			if(ncList.size()==0) {
				Leansnews ln=new Leansnews(newsId, newsTitle, newsContent,newsCatalogueId);
				int n=nDao.updateLeansnewsById(ln);
				if(n>0) {
					out.print(Result.toClient(true, "修改成功",""));
				}else {
					out.print(Result.toClient(false, "修改失败",""));
				}
			}else {
				out.print(Result.toClient(false, "标题已重复",""));
			}
		}

		out.flush();
		out.close();
	}
	//localhost:8080/huaduoyoupin/adminNews?reqName=delectNewsByIds
	//批量删除
	public void delectNewsByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		LeansnewsDaoImpl lDao=new LeansnewsDaoImpl();
		//设置编码集格式
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();//创建out对象
		String ids=request.getParameter("ids");
		int n=lDao.delectProductByIds(ids);
		//调用实现类返回受影响行数
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		out.flush();
		out.close();
	}

}
